import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createHtmlPlugin } from 'vite-plugin-html'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
  server: {
    host: '0.0.0.0',
    proxy: {
      '/api/': {
        // target: 'https://mp-debug.devld.cn/admin',
        target: 'http://localhost:8888/',
        rewrite: (p) => p.replace(/^\/api/, ''),
        changeOrigin: true
      }
    }
  },
  plugins: [
    vue(),
    createHtmlPlugin({ inject: { data: loadEnv(mode, __dirname) } }),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
}))
